#include "Circular_queue.hpp"
#include <unistd.h>
#include <time.h>

void* comsumer(void * args)
{
    Circular_queue<int> * cq = (Circular_queue<int> *)args;
    while(true)
    {
        int ret = 0;
        cq->pop(ret);
        //cout<<pthread_self()<<"  消费成功 :"<<ret<<endl;
        sleep(1);
    }
}

void* produtor(void * args)
{
    Circular_queue<int> * cq = (Circular_queue<int> *)args;
    while(true)
    {
        int x = rand() % 500;
        cq->push(x);
        //cout<<pthread_self()<<"生产成功 :"<<x<<endl;
        //sleep(1);
    }
}

int main()
{
    srand((unsigned long)time(nullptr));
    pthread_t c[3],p[2];
    Circular_queue<int> * cq = new Circular_queue<int>();
    for(int i = 0;i<3;i++) pthread_create(c+i,nullptr,comsumer,(void*)cq);
    for(int i = 0;i<2;i++) pthread_create(p+i,nullptr,produtor,(void*)cq);

    for(int i = 0;i<3;i++) pthread_join(c[i],nullptr);
    for(int i = 0;i<2;i++) pthread_join(p[i],nullptr);
    delete cq;
    return 0;
}